<template>
    <!-- 组件的模板 -->
    <div class="page">
        <div class="weui-cells weui-cells_radio">
            <template v-for="item in items">
                <label class="weui-cell weui-check__label" :for="'x'+item.team_id" @click="goBackMyIndex(item.team_id,item.team_name)">
                    <div class="weui-cell__bd">
                        <p>{{item.team_name}}</p>
                    </div>
                    <div class="weui-cell__ft">
                        <input type="radio" class="weui-check" name="radio1" :id="'x'+item.team_id" :checked="item.team_id == teamid">
                        <span class="weui-icon-checked"></span>
                    </div>
                </label>
            </template>
            
        </div>
        <a class="add-item touch com-border-bottom com-border-top" href="javascript:;" @click="createTeam"><span class="icon bg_sprite"></span><span>新建企业</span></a>
    </div>  
        
</template>
 
<script>  
    /**
     *  引入工具类库 或 Native API
     *  example : import widget from 'gapi/widget'
     */
    import widget from 'gapi/widget.js'
    import * as util from 'js/util.js'
    import cache from 'gapi/cache.js'
    import commonPage from 'clientApi/commonPage.js'
    // commonPage.canPullWebView(true)
    /**
     *  引入组件 以及 组件接口(Action)
     *  example : import Somecomponents from 'components/Somecomponents/Somecomponents'
     */ 

    let offset = 1 // 返回记录行的偏移量(从第几条数据开始取
    let rows = 20 // 返回记录行的最大条数（取的条数）
    let canLoadMore = 1
    /**
     *  组件实例对象
     */
    export default {
        components: {

        },
        data: function(){
            return{
                user_id:'',
                items:[],
                teamid:'',
                dmUserId:'',
                backWhereParam:''
            }
        },
        vuex: { 
            getters: {

            },
            actions: {

            }
        },
        methods: {
            /**
             * 初始化
             * @method init
             * @public
             * @return {Null} void
             */
            init (){
                this.teamid = util.getUrddataString('team_id')
                this.dmUserId = util.getUrddataString('dmUserId')
                this.backWhereParam = util.getUrddataString('backWhereParam')
                this.user_id = util.getUrddataString('user_id')
                this.getListData()
                window.loadData = () => {
                    commonPage.dataDownloadFinished()
                    offset = 1
                    this.getListData(2)
                }
            },
            // 获取企业列表数据
            getListData (mark){
                commonPage.loadPageStatus('loading')
                util.ajax({
                    url:'/team/list/detail',
                    type:'GET',
                    data:{
                        team_id:0,
                        project_id:0,
                        offset:offset,
                        rows:rows
                    },
                    timeout:10000,
                    success:(obj) => {
                        console.log(obj)
                        // commonPage.dataDownloadFinished()
                        commonPage.loadPageStatus('loadSuccess')
                        if(obj&&obj.errno == 0){
                            if(obj.data.length){
                                if(mark){
                                    this.items = obj.data
                                }else{
                                    obj.data.forEach((item)=>{
                                        // if(item.team_id == this.teamid){
                                        //     this.items.unshift(item)
                                        // }else{
                                        //     this.items.push(item)
                                        // }
                                        this.items.push(item)
                                    })
                                }
                                offset+=rows
                            }else{
                                if(mark && obj.data.length == 0){
                                    this.items = obj.data
                                }
                                canLoadMore = 0
                            }
                        }else{
                            widget.toast(obj.errmsg)
                        }
                    },
                    error: (xhr, status) => {
                        // commonPage.dataDownloadFinished()
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('服务器异常')
                    },
                    noNetwork: () => {
                        // commonPage.dataDownloadFinished()
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
            },
            // 返回到‘我’页面
            goBackMyIndex (team_id,team_name){
                let selectTeamObj = {
                    team_id:team_id,
                    team_name:team_name
                }
                cache.getMemoryCache('myIndexwebViewID', (data) => {
                    if(data){
                        commonPage.goBack(1, {'key':'selectTeam','value':selectTeamObj}, data.webViewID)
                    }
                })
            },
            // 新建企业
            createTeam (){
                let url = `backWhereParam=${this.backWhereParam}&dmUserId=${this.dmUserId}&user_id=${this.user_id}`
                location.href = "dmsaas://CreateTeam?urdData="+encodeURIComponent(url)
            }
        },
        created(){
            this.init();
            commonPage.registerHitPageBottomListener(() => {
                if(canLoadMore==0){
                    return
                }
                this.getListData()
            }, 0)

            commonPage.getWebViewID((data)=>{
                cache.setMemoryCache('teamListWebViewId',{'webViewID':data})
            })
 
        }
    }

    
</script>

<!-- CSS 样式 -->
<style src="../../node_modules/weui/dist/style/weui.css"></style>
<style src="../../static/css/common.css"></style>
<style src="../../static/css/team.css"></style>



